package org.spindle.dungeon.rooms;

import org.spindle.dungeon.EntityManager;
import org.spindle.dungeon.entities.GeneralEntity;
import org.spindle.dungeon.entities.Tile;
import org.spindle.dungeon.entities.WallEntity;

public abstract class GeneralRoom {

    protected int x = 0;
    protected int y = 0;

    public GeneralRoom(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public abstract int[][] getRoom();


    public void generate() {
        int[][] room = getRoom();
        GeneralEntity ent;
        for (int j = 1; j < room.length - 1; j++) {
            for (int i = 1; i < room[j].length - 1; i++) {
                ent = null;
                if (room[j][i] > 0) {
                    ent = new WallEntity();
                    ent.updateBy(getArrayRect(room, i, j), room[j][i]);

                } else {
                    ent = new Tile();
                }

                if (ent != null) {
                    ent.x = x + 16 * i;
                    ent.y = y + 16 * j;
                    EntityManager.getInstance().registerEntity(ent);
                }
            }
        }

    }

    public String getArrayRect(int[][] array, int x, int y) {
        StringBuffer buf = new StringBuffer();

        for (int i = y - 1; i <= y + 1; i++)
            for (int j = x - 1; j <= x + 1; j++)
                buf.append(array[i][j]);

        return buf.toString();
    }

}
